//
//  VideoTableViewController.m
//  网易新闻
//
//  Created by lanou3g on 15/10/27.
//  Copyright © 2015年 马文强. All rights reserved.
//

#import "VideoTableViewController.h"
#import "VideoListTableViewCell.h"
#import "netWorkHandle.h"
#import "VideoModel.h"
#import "UIImageView+WebCache.h"
#import "VideoDetailViewController.h"
#import "MJRefresh.h"

@interface VideoTableViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *VideoDataArray;

@property (assign, nonatomic) int PagNum;

@end

@implementation VideoTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self loadtableView];
    [self loadData];
    [self loadMoreData];
    
    
}
#pragma mark ---- 刷新 加载
- (void)loadData
{
    __weak __typeof(self) weakSelf = self;
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _PagNum = 0;
        [weakSelf loadNetWorkData:[NSString stringWithFormat:@"%d", _PagNum]];
     }];
    [self.tableView.header beginRefreshing];
}


- (void)loadMoreData
{
    
        __weak __typeof(self) weakSelf = self;
        self.tableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
            _PagNum += 10;
            [weakSelf loadNetWorkData:[NSString stringWithFormat:@"%d", _PagNum]];
            
        }];
}

- (NSMutableArray *)VideoDataArray
{
    if (!_VideoDataArray) {
        _VideoDataArray = [NSMutableArray array];
    }
    return _VideoDataArray;
}



- (void)loadNetWorkData :(NSString *)str
{
    NSString *string = [NSString stringWithFormat:@"http://c.3g.163.com/nc/video/home/%@-10.html",str];
    if ([str isEqualToString:@"0"]) {
        [self cleanDataArray];
    }
    [netWorkHandle getDataWithURLString:string compare:^(id object) {
        NSDictionary *dic = object;
        NSArray *arr = [dic objectForKey:@"videoList"];
        for (NSDictionary *d in arr) {
             VideoModel *model = [[VideoModel alloc] init];
            [model setValuesForKeysWithDictionary:d];
            [self.VideoDataArray addObject:model];
         }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
     }];
}


// 清除缓存
- (void)cleanDataArray
{
    _VideoDataArray = nil;
}


- (void)loadtableView
{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    UINib *nib = [UINib nibWithNibName:@"VideoListTableViewCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"CELL"];
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _VideoDataArray.count;
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    VideoListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    VideoModel *model = _VideoDataArray[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoModel *model = _VideoDataArray[indexPath.row];
    VideoDetailViewController *playVC = [[VideoDetailViewController alloc] init];
    playVC.mp4_url = model.mp4_url;
    [self.navigationController presentViewController:playVC animated:YES completion:^{
    }];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 280;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
